package com.wolf.etl.service.impl;

import com.wolf.etl.model.EtlTableModel;
import com.wolf.etl.service.IEtlTableService;
import com.wolf.provider.order.enumerate.QUERY_TYPE;
import com.wolf.provider.order.info.PageBean;
import com.wolf.provider.order.info.PageInfo;
import com.wolf.provider.order.info.ParamBean;
import com.wolf.provider.order.service.impl.BaseServiceImpl;
import com.wolf.util.NumberUtil;
import com.wolf.vo.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tommy's father on 2017/8/17.
 */
@Service("table_service")
public class EtlTableServiceImpl extends BaseServiceImpl<Long> implements IEtlTableService {

    private static Logger logger = LoggerFactory.getLogger(EtlTableServiceImpl.class);

    private static final String logger_prefix = "wolf-etl#com.wolf.etl.service.impl#EtlTableServiceImpl";

    @Override
    public List<EtlTableModel> findAllMasterEtlTableModelsByDefineId(Long configId) {
        StringBuilder sql = new StringBuilder();
        sql.append(" select * from etl_table t where t.etl_define_id=").append(configId);
        sql.append(" and t.is_master=1 and t.status=1 ");
        Map<String, Object> map = new HashMap<>();
        List<EtlTableModel> models = findModelListByMap(sql.toString(), map, new EtlTableModel());
        return models;
    }

    @Override
    public List<EtlTableModel> findAllExtTableModelsById(Long tabId) {
        StringBuilder sql = new StringBuilder();
        sql.append(" select t1.* from etl_table t,etl_table_rel r,etl_table t1 ");
        sql.append("  where 1=1 ");
        sql.append("  and t1.id = r.rel_table_id  and t.id = r.master_table_id ");
        sql.append("  and t1.is_master=0 and t1.status = 1 ");
        sql.append("  and t.id=").append(tabId);
        sql.append("  nd t.is_master=1 and t.status=1 ");
        Map<String, Object> map = new HashMap<>();
        List<EtlTableModel> models = findModelListByMap(sql.toString(), map, new EtlTableModel());
        return models;
    }

    @Override
    public List<EtlTableModel> findMasterEtlTableModels(Long defId) {
        StringBuilder sql = new StringBuilder();
        sql.append(" select * from etl_table t where t.etl_define_id =").append(defId);
        sql.append(" and t.is_master=1 and t.status=1 ");
        Map<String, Object> map = new HashMap<>();
        List<EtlTableModel> models = findModelListByMap(sql.toString(), map, new EtlTableModel());
        return models;
    }

    @Override
    public EtlTableModel findById(Long id) {

        StringBuilder sql = new StringBuilder("SELECT * from etl_table where 1=1 and status=1 ");
        sql.append(" and id=").append(id);
        Map<String, Object> map = new HashMap<>();

        List<EtlTableModel> vos = findModelListByMap(sql.toString(), map, new EtlTableModel());

        if (vos == null || vos.size() == 0) {
            return null;
        }
        return vos.get(0);
    }

    @Override
    public void save(EtlTableModel model) {
        if (model.getId() == null) {
            insertModel(model);
        } else {
            updateModel(model);
        }
    }

    @Override
    public PageResult findByPage(EtlTableModel model, PageBean pageBean) {

        StringBuilder sql = new StringBuilder();
        sql.append("select * from etl_table t where 1=1 ");

        List<ParamBean> paramBeans = new ArrayList<ParamBean>();

        if (NumberUtil.isNotEmpty(model.getClient_id())) {
            ParamBean paramBean = new ParamBean();
            paramBean.setWhereField("t.client_id");
            paramBean.setField("client_id");
            paramBean.setValue(model.getClient_id());
            paramBean.setType(QUERY_TYPE.EQUAL.getType());
            paramBeans.add(paramBean);
        }

        PageInfo<EtlTableModel> vos = findByPageInfo(sql.toString(), paramBeans, model, pageBean);

        PageResult result = new PageResult();
        result.buildResult(vos);
        return result;
    }

}